package com.ly.emp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.constant.EmpErrorConstants;
import com.ly.emp.entity.HubTeamEvaluateIndex;
import com.ly.emp.entity.HubTeamTemplate;
import com.ly.emp.mapper.HubTeamEvaluateIndexMapper;
import com.ly.emp.service.IHubTeamEvaluateIndexService;
import com.ly.emp.service.IHubTeamTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 综合评价指标管理-impl
 *
 * @author luoyun
 * @Date: 2025-05-20
 */
@Service
@RequiredArgsConstructor
public class HubTeamEvaluateIndexServiceImpl extends ServiceImpl<HubTeamEvaluateIndexMapper, HubTeamEvaluateIndex> implements IHubTeamEvaluateIndexService {

    private final IHubTeamTemplateService hubTeamTemplateService;

    @Override
    public R getId(String id) {
        HubTeamEvaluateIndex entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamEvaluateIndex entity) {
        List<HubTeamEvaluateIndex> list = this.list(
                new LambdaQueryWrapper<HubTeamEvaluateIndex>()
                        .orderByAsc(HubTeamEvaluateIndex::getDisplayOrder)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamEvaluateIndex entity, Page<HubTeamEvaluateIndex> page) {
        String orgCode = entity.getOrgCode();
        String type = entity.getType();
        String templateId = entity.getTemplateId();
        if (StrUtil.isBlank(orgCode)) {
            return R.fail(EmpErrorConstants.NO_TEAM);
        }
        if (StrUtil.isBlank(templateId) && StrUtil.isBlank(type)) {
            return R.fail("请选择模版");
        }
        HubTeamTemplate one = StrUtil.isNotBlank(templateId) ?
                hubTeamTemplateService.getById(templateId) :
                hubTeamTemplateService.getOne(
                        new LambdaQueryWrapper<HubTeamTemplate>()
                                .eq(HubTeamTemplate::getOrgCode, orgCode)
                                .eq(HubTeamTemplate::getType, type)
                );
        if (one == null) {
            return R.fail("找不到模版信息");
        }
        entity.setTemplateCode(one.getTemplateCode());
        IPage<HubTeamEvaluateIndex> pageList = this.page(page,
                new LambdaQueryWrapper<HubTeamEvaluateIndex>()
                        .eq(HubTeamEvaluateIndex::getOrgCode, orgCode)
                        .eq(StrUtil.isNotBlank(entity.getTemplateCode()), HubTeamEvaluateIndex::getTemplateCode, entity.getTemplateCode())
                        .eq(StrUtil.isNotBlank(entity.getIndexCode()), HubTeamEvaluateIndex::getIndexCode, entity.getIndexCode())
                        .eq(StrUtil.isNotBlank(entity.getPeriodCode()), HubTeamEvaluateIndex::getPeriodCode, entity.getPeriodCode())
                        .like(StrUtil.isNotBlank(entity.getIndexName()), HubTeamEvaluateIndex::getIndexName, entity.getIndexName())
                        .orderByAsc(HubTeamEvaluateIndex::getDisplayOrder)
                        .orderByDesc(HubTeamEvaluateIndex::getSysUpdateDate));
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamEvaluateIndex entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamEvaluateIndex entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamEvaluateIndex> dto) {
        List<HubTeamEvaluateIndex> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }
}
